package vetAdmin.listeners;

import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

//table.getModel().addTableModelListener(new MyTableModelListener(table));

public class ClientesTableListener implements TableModelListener {
    JTable table;

    // It is necessary to keep the table since it is not possible
    // to determine the table from the event's source
    public ClientesTableListener(JTable table) {
        this.table = table;
    }

    public void tableChanged(TableModelEvent e) {
    	
    	int firstRow = e.getFirstRow();
    	int lastRow = e.getLastRow();
    	int index = e.getColumn();

        switch (e.getType()) {
        
        case TableModelEvent.INSERT:
          for (int i = firstRow; i <= lastRow; i++) {
            System.out.println(i);
          }
          break;
          
        case TableModelEvent.UPDATE:
          if (firstRow == TableModelEvent.HEADER_ROW) {
            if (index == TableModelEvent.ALL_COLUMNS) {
              System.out.println("A column was added");
            } else {
              System.out.println(index + "in header changed");
            }
          } else {
            for (int i = firstRow; i <= lastRow; i++) {
              if (index == TableModelEvent.ALL_COLUMNS) {
                System.out.println("All columns have changed");
              } else {
                System.out.println(index);
              }
            }
          }
          break;
          
        case TableModelEvent.DELETE:
          for (int i = firstRow; i <= lastRow; i++) {
            System.out.println(i);
          }
          break;
        }
      }
    }